package com.shisfish.core.Test;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class GetMARC {

	private static BufferedReader bw;

	public static String codeString(String fileName) throws Exception{  
	    BufferedInputStream bin = new BufferedInputStream(  
	    new FileInputStream(fileName));  
	    int p = (bin.read() << 8) + bin.read();  
	    String code = null;  
	      
	    switch (p) {  
	        case 0xefbb:  
	            code = "UTF-8";  
	            break;  
	        case 0xfffe:  
	            code = "Unicode";  
	            break;  
	        case 0xfeff:  
	            code = "UTF-16BE";  
	            break;  
	        default:  
	            code = "GBK";  
	    } 
	    bin.close();
	      
	    return code;  
	}
	
	public static void main(String[] args) throws Exception {
		String ss = null;
		List<TestPo> list = new ArrayList<TestPo>();
		try {
			File file = new File("F:\\276663.iso");
			FileOutputStream out = new FileOutputStream(new File(
					"F:\\test1.txt"), true);
			String code = codeString("F:\\276663.iso");
			FileInputStream fInputStream = new FileInputStream(file);
			InputStreamReader inputStreamReader = new InputStreamReader(fInputStream, code);  
			BufferedReader in = new BufferedReader(inputStreamReader); 
			
			bw = new BufferedReader(new FileReader(file));
			boolean flag = true;
			int[] temps = null;
			while ((ss = bw.readLine()) != null) {

				String[] mark = ss.split(":");
				int sum = 0;
				TestPo po = new TestPo();
				int[] temp = new int[mark.length - 1];
				temps = new int[mark.length - 1];
				for (int i = 1; i < mark.length; i++) {
					temp[i - 1] = Integer.parseInt(mark[i].trim());
					sum += temp[i - 1];
					if (flag) {
						temps[i - 1] = 0;
					}
				}
				flag = false;
				po.setMark(temp);
				po.setName(mark[0]);
				po.setAvg(sum / (mark.length - 1));
				list.add(po);
				out.write(("\t\n" + ss).getBytes());
			}

			bw.close();
			out.close();
			in.close();

			int row = list.size();
			int[][] marks = new int[row + 1][];
			for (int i = 0; i < row; i++) {
				marks[i] = list.get(i).getMark();
			}
			marks[row] = temps;
			int sumTemp = 0;
			int count = list.get(0).getMark().length;
			for (int i = 0; i < count; i++) {
				for (int j = 0; j < row; j++) {
					sumTemp += marks[j][i];
				}
				marks[row][i] = sumTemp / row;
				sumTemp = 0;
			}
			for (int[] is : marks) {
				for (int i : is) {
					System.out.print(i + " ");
				}
				System.out.println();
			}

		} catch (IOException e) {
			e.printStackTrace();
		}

		for (TestPo po : list) {
			System.out.println("Name:" + po.getName() + " AVG:" + po.getAvg());
		}
	}

}

class TestPo {

	private String name;
	private Integer avg;
	private int[] mark;

	public int[] getMark() {
		return mark;
	}

	public void setMark(int[] mark) {
		this.mark = mark;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAvg() {
		return avg;
	}

	public void setAvg(Integer avg) {
		this.avg = avg;
	}

}
